from flask import Flask, Blueprint, render_template, request, redirect, session, flash, jsonify
import pymysql
from ..utils import db

fd = Blueprint(name="feedback", import_name=__name__, url_prefix='/')


@fd.route("/feedback", methods=['GET', 'POST'])
def feedback():
    if request.method == 'GET':
        if 'user_info' in session:
            user_info = session['user_info']
            print(user_info)
            return render_template("feedback.html", user_info=user_info)
        else:
            return render_template("feedback.html")

    userName = request.form['userName']
    userEmail = request.form['userEmail']
    feedbackType = request.form['feedbackType']
    feedbackContent = request.form['feedbackContent']
    if userName and userEmail and feedbackType and feedbackContent:
        db.insert_db_feedback(userName, userEmail, feedbackType, feedbackContent)
        return jsonify({"message": "感谢您的反馈！我们将尽快处理。"})
    else:
        return jsonify({"error": "请填写所有字段后再提交。"}), 400



